package com.huangyan.controller;

import com.huangyan.domin.User;
import com.huangyan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by huangyan on 2017/6/21.
 */
@Controller
public class LoginController {

    @Autowired
    UserService userService;
    User user;

    @RequestMapping(value = "/login",method= RequestMethod.POST)
    public ModelAndView login(HttpServletRequest request, String username, String password)
    {
        boolean match=userService.hasMatchUser(username,password);
        if(!match)
            return new ModelAndView("login");
        else
        {
            user=userService.findUserByUserName(username);
            Date last_time=new Date();
            String last_ip=request.getLocalAddr();
            user.setLast_time(last_time);
            user.setLast_ip(last_ip);
            userService.loginSuccess(user);
            return new ModelAndView("main");
        }
    }

}
